﻿#ifndef Base_MessageQueue_h__
#define Base_MessageQueue_h__

#include <queue>
#include <mutex>
#include <memory>
class Base_Context;
class Base_Monitor;

struct Base_Message
{
	uint32_t des;
	uint32_t src;
	uint32_t session;
	void *data;
	uint32_t type;
	int	 size;

	Base_Message() {};
	Base_Message(uint32_t des,uint32_t src,uint32_t session,void* data,uint32_t type,int size)
		:des(des),src(src),session(session),data(data),type(type),size(size)
	{
		
	}
};

class Base_MessageQueue
{

public:
	Base_MessageQueue(uint32_t handle,Base_Context * context);
	~Base_MessageQueue();

	void mq_push(const Base_Message& msg);
	int  mq_pop(Base_Message & msg);
	Base_Context * get_context() const;
	void set_in_gmq_flag(bool in);
	bool get_in_gmq_flag();
private:
	std::mutex m_mutex_queue;
	std::mutex m_mutex_in_gmq_state;
	std::queue<Base_Message>* m_queue;
	uint32_t	 m_handle;
	Base_Context * m_context;
	bool   m_inGmq;   //在全局队列中 
};


class Base_GlobalQueue
{
public:
	
	static Base_GlobalQueue* get_global_queue_instance();
	
	void global_queue_push(Base_MessageQueue* mq);
	Base_MessageQueue* global_queue_pop();
private:
	Base_GlobalQueue(const Base_GlobalQueue&) = delete;
	Base_GlobalQueue& operator=(const Base_GlobalQueue &) = delete;

	Base_GlobalQueue();
	~Base_GlobalQueue();

	std::mutex m_mutex;
	std::queue<Base_MessageQueue*>* m_queue;
	Base_Monitor * m_monitor;
	
};
#endif // Base_MessageQueue_h__
